home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.09 Sep 96 / tips.txt
Encoding:
INI File  |  1997-05-07  |  7.3 KB  |  164 lines  |  [TEXT/R*ch]

  1. [tip of the month]
  2. MAKING SURE YOU'RE TAKING OUT THE GARBAGE
  3. A somewhat obscure, but useful way of determing whether objects are getting deleted in C++ is to do this:
  4. 1.  In the class implementation (usually in the header file), add a destructor:
  5.  
  6. class MyClass
  7. {
  8. public:
  9.    virtual ~MyClass();
  10.  
  11. 2.  Don't implement the destructor; i.e., don't write the actual code for the destructor method.
  12. 3.  Run the project.  If you get link errors for the destructors, this means that the destructors are getting called (i.e.  the objects are getting deleted), which is what you want.  If you don't get a link error, this means that the object never gets deleted, which could signify a possible memory leak.
  13. Jeremy Vineyard
  14.  
  15. PRE-FLIGHTING EDITTEXT
  16. For a program I recently wrote, I wanted to do user input checking for a dialog box as the user was typing into an EditText field.  I wrote a routine that figures out what the contents of a specific EditText box will be if the current event is allowed to be processed.  I then called the routine from my custom dialog event proc, and analyzed the results.  That way, I could easily decide if the user's action would produce valid results, or if I needed to abort the current event.
  17. IsDlgControl is a simple check for control characters we always want to process.  DivineNewItemString is the routine that does all the work.  PStrCopy is a little routine for copying Pascal strings; I'm sure other people have better ways of doing this.
  18. Michael Trent
  19.  
  20. // some control key constants.
  21. #define kEnterKey               3
  22. #define kBackspace              8
  23. #define kTab                    9
  24. #define kReturnKey              13
  25. #define kEscapeKey              27
  26. #define kLeftKey                28
  27. #define kRightKey               29
  28. #define kUpKey                  30
  29. #define kDownKey                31
  30. #define kDelete                 0xFF
  31.  
  32. /* IsDlgControl
  33.  * Returns true if c is a special control key (say an arrow key, or escape).
  34.  * Otherwise returns false.
  35.  */
  36. Boolean IsDlgControl(char c)
  37. {
  38.         if ((c >= kEscapeKey) && (c <= kDownKey)) return true;
  39.         if ((c == kReturnKey) || (c == kEnterKey) || (c == kDelete)
  40.                 || (c == kBackspace) || (c == kTab)) return true;
  41.  
  42.         return false;
  43. }
  44.  
  45. /* PStrCpy
  46.  * A little routine to copy Pascal strings. Provided for those people who don't
  47.  * already use BlockMove() to do this for them ...
  48.  */
  49. void PStrCpy(Str255 s, const Str255 t)
  50. {
  51.         short i;
  52.  
  53.         for (i=0; i<=s[0]; i++) {
  54.                 s[i]=t[i];
  55.         }
  56. }
  57.  
  58. /* DivineNewItemString
  59.  * Given a DialogPtr, EventPtr and an item number for the active EditText DLOG
  60.  * Item, it returns what the string will be if the current event is processed.
  61.  * It should be called from a custom dialog event proc.
  62.  */
  63. void DivineNewItemString (DialogPtr d, EventRecord *e, short item, Str255
  64. output)
  65. {
  66.         short                   *TEScrpLength = (short *)0x0AB0;
  67.         DialogRecord    *dr;
  68.         TEHandle                teh;
  69.         char                    c;
  70.         Str255                  input, text;
  71.         short                   selStart, selEnd;
  72.         short                   i;
  73.         short                   outStrIdx=0;
  74.  
  75.         short                   iType;
  76.         Handle                  iHandle;
  77.         Rect                    iRect;
  78.  
  79.         // get the text string
  80.         GetDItem(d,item,&iType,&iHandle,&iRect);
  81.         GetIText(iHandle,text);
  82.  
  83.         // Set the input string
  84.         c = (e->message & charCodeMask);
  85.  
  86.         if (IsDlgControl(c)) {  // if it's a control char, return the
  87. item's text.
  88.                 PStrCpy(output,text);
  89.                 return;
  90.         } else if (e->modifiers & cmdKey) {
  91.                 if  ((c == 'v') || (c == 'V')) {        // if pasting, get
  92. the pasted string.
  93.                         (void)TEFromScrap();
  94.                         HLock(iHandle);
  95.                         iHandle = TEScrapHandle();
  96.                         for (i=0; i< *TEScrpLength; i++) {
  97.                                 input[i+1]=((unsigned char *)(*iHandle))[i];
  98.                         }
  99.                         input[0]=*TEScrpLength;
  100.                         HUnlock(iHandle);
  101.                 } else { // if any other command stroke
  102.                         PStrCpy(output,text);
  103.                         return;
  104.                 }
  105.         } else {
  106.                 // else, set the input string equal to the new character
  107.                 input[0]=1;
  108.                 input[1]=(unsigned char) c;
  109.         }
  110.  
  111.         // get the selection point from the TERec
  112.         dr = (DialogRecord *)d;
  113.         teh = dr->textH;
  114.         selStart=(*teh)->selStart;
  115.         selEnd=(*teh)->selEnd;
  116.  
  117.         // generate output string:
  118.         //  copy the first bit of text
  119.         for (i=1; i<=selStart; i++) {
  120.                 output[++outStrIdx]=text[i];
  121.         }
  122.         //  copy the input string
  123.         for (i=1; i<=input[0]; i++) {
  124.                 output[++outStrIdx]=input[i];
  125.         }
  126.         //  copy the last part of text
  127.         for (i=selEnd+1; i<=text[0]; i++) {
  128.                 output[++outStrIdx]=text[i];
  129.         }
  130.         //  lastly, set the length
  131.         output[0] = outStrIdx;
  132. }
  133.  
  134. AVOIDING THE MENU ITEM GOTCHA
  135. Watch out when using AppendMenu or InsertMenuItem.  I'm using AppendMenu to create a popup menu based on user input.  Since AppendMenu interprets certain characters as menu attributes (such as command-key equivalents, menu dimming, etc.), if a user enters one of these characters, you will run into a problem with your menu.  To avoid this, call AppendMenu or InsertMenuItem with a dummy string such as "\p " (the string must not be empty), and then call SetMenuItemText using the real string, like so:
  136.  
  137. Str255 theUserName;
  138.  
  139. // Get the string to display in the menu
  140. theUser->GetUserName (theUserName);
  141.  
  142. // Append an empty string to the menu
  143. AppendMenu (theMenuHandle, "p\ ");
  144.  
  145. // Change the menu item to the userName
  146. SetMenuItemText (theMenuHandle, theIndex, theUserName);
  147.  
  148. Tim Pedone
  149.  
  150. A FREE TOOL FROM APPLE
  151. You can use the Finder's "About this Macintosh" window to see the size your application's heap, and the amount of free memory that is available in your heap.  This information is only updated during idle time, though, so don't count on it to always be accurate.  [Here's the real tip: use Balloon Help to get a numeric representation of the information. - sgs]
  152. David Lawrence
  153.  
  154. WORLD'S MOST ORIGINAL USE OF RESEDIT
  155. If you are looking in the code of a PICT, you can often see a color table with 256 colors even if you use only 2 or 3 colors.  This can represent a massive waste of disk space.  I tried to find an app to compact the color table but I couldn't find one - until finaly I found ResEdit!
  156. My trick: create a 'cicn' resource in ResEdit, paste in your PICT or draw it, select the part for using in the PICT, copy and paste it into an actual PICT.  The only limitation is the maximum size: 64 by 64.
  157. Henri Clerc
  158.  
  159. NO, WAIT - WORLD'S MOST TRULY TOTALLY ORIGINAL USE OF RESEDIT
  160. This might be one everybody knows, but I just figured it out and it's saved me a load of time, so here goes.
  161. ResEdit is a Drag-and-Drop application!!!  This works for any type of file.  No longer must eons be frittered away navigating through countless folders in open dialog boxes to access resource forks of files hidden deep within the murky depths of your hard drive.  Just do a Find in the Finder and drag it to ResEdit - I keep an alias on my desktop.  Happy hacking!
  162. Joshua "Vampiric Bunny" Glazer
  163.  
  164.